﻿//
// Created by Administrator on 2022/6/16.
//
#include "windows.h"
#include "cstdio"
#include "resource.h"

BOOL UseCustomResource()
{
    // 定位资源
    HRSRC hRsrc = FindResource(NULL, MAKEINTRESOURCE(IDB_BITMAP1), "binary");
    if (NULL == hRsrc)
    {
        MessageBox(0,"找不到资源", 0,0);
        return FALSE;
    }
    // 获取资源大小
    DWORD dwSize = SizeofResource(NULL, hRsrc);
    if (0 == dwSize)
    {
        MessageBox(0,"获取资源大小错误", 0,0);
        return FALSE;
    }
    // 加载资源到内存中
    HGLOBAL hGlobal = LoadResource(NULL, hRsrc);
    if (NULL == hGlobal)
    {
        MessageBox(0,"加载资源失败", 0,0);
        return FALSE;
    }
    // 获取资源数据指针
    LPVOID pBuffer = LockResource(hGlobal);
    if (NULL == pBuffer)
    {
        MessageBox(0,"获取资源指针失败", 0,0);
        return FALSE;
    }
    // 写入磁盘
    FILE *fp = fopen("c:\\out.exe", "wb");
    if (NULL != fp)
    {
        if (dwSize != fwrite(pBuffer, 1, dwSize, fp))
        {
            MessageBox(0,"写入文件失败", 0,0);
            fclose(fp);
            return FALSE;
        }
        fclose(fp);
    }
    MessageBox(0,"成功", 0,0);
    return TRUE;
}

static BOOL test_access_resource(){
    //LoadIcon(hInstance, (LPCTSTR)IDI_SCREENCAPTURE);
    HINSTANCE hInstance = GetModuleHandle(NULL);
    //hsrc = FindResource( hInst , MAKEINTRESOURCE(dwRID), (LPCTSTR)"wav ");
    //HRSRC hRsrc = FindResource(NULL, MAKEINTRESOURCE(IDB_BITMAP1), "BITMAP");

    HCURSOR cursor = LoadCursor(hInstance, MAKEINTRESOURCE(IDC_MYAPP_POINTER));

    HRSRC hRsrc = FindResource( hInstance, MAKEINTRESOURCE(IDR_DATA2), "DATA" );
    hRsrc = FindResource( hInstance, MAKEINTRESOURCE(IDB_PNG1), "PNG" );
    if (NULL == hRsrc)
    {
        MessageBox(0,"找不到资源", 0,0);
        return FALSE;
    }
    // 获取资源大小
    DWORD dwSize = SizeofResource(NULL, hRsrc);
    if (0 == dwSize)
    {
        MessageBox(0,"获取资源大小错误", 0,0);
        return FALSE;
    }
    // 加载资源到内存中
    HGLOBAL hGlobal = LoadResource(NULL, hRsrc);
    if (NULL == hGlobal)
    {
        MessageBox(0,"加载资源失败", 0,0);
        return FALSE;
    }
    // 获取资源数据指针
    LPVOID pBuffer = LockResource(hGlobal);
    if (NULL == pBuffer)
    {
        MessageBox(0,"获取资源指针失败", 0,0);
        return FALSE;
    }
}

int main( int argc, char* argv[]){
    test_access_resource();
    return 0;
}